<template>
  <div>
    <el-container>
      <el-header class="nav-bar">
        <div style="display: flex; justify-content: center; align-items: center; height: 100%;">
          <form method="get" target="_blank" action="/modules/article/search.php"
                style="display: flex; align-items: center;margin-left: 40%">
            <el-input
                style="width: 300px; margin-right: 10px;"
                placeholder="输入少字也别输入错字"
                type="text"
                v-model="name"
            ></el-input>
            <el-button type="primary" icon="el-icon-search" @click.prevent="getSou"></el-button>
          </form>
          <div style="display: flex; justify-content: space-between; align-items: center; width: 100%;">
            <i class="el-icon-quanping" style="font-size: 26px;" @click="handleFull"></i>
            <el-dropdown placement="bottom">
              <div v-if="user" style="display: flex;align-items: center;cursor: default;">
                <img :src="user.avatar" alt=""
                     style="border-radius:50%;width: 40px;height: 40px;margin:0 5px">
                <span>{{ user.name }}</span>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="$router.push('/user')">
                  个人信息
                </el-dropdown-item>
                <el-dropdown-item @click.native="$router.push('/houtai')" v-if="user.role === '管理员'">
                  后台管理
                </el-dropdown-item>
                <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
                <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <!-- 其他头部内容 -->

        <div style="display: flex; justify-content: center; align-items: center; width: 100%;">
          <el-tabs v-model="activeName" @tab-click="handleClick" class="nav_cont">
            <el-tab-pane label="首页" name="homePage"></el-tab-pane>
            <el-tab-pane label="玄幻小说" name="first"></el-tab-pane>
            <el-tab-pane label="修真小说" name="second"></el-tab-pane>
            <el-tab-pane label="都市小说" name="third"></el-tab-pane>
            <el-tab-pane label="历史小说" name="fourth"></el-tab-pane>
            <el-tab-pane label="网游小说" name="wangyou"></el-tab-pane>
            <el-tab-pane label="科幻小说" name="scienceFiction"></el-tab-pane>
            <el-tab-pane label="我的书架" name="shujia"></el-tab-pane>
          </el-tabs>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'HomeView',
  data() {
    return {
      name: '',
      activeName: this.$route.name,
      collapseIcon: 'el-icon-s-fold',
      breadcrumbs: [],
      user: JSON.parse(localStorage.getItem("honey-user") || '{}'),
    };
  },
  watch: {
    $route(to) {
      this.activeName = to.name;
    }
  },
  methods: {
    handleClick(tab) {
      if (tab.name === '我的书架') {
        this.$router.push({path: '/shujia'})
        return
      }
      if (tab.name !== this.$route.name) {
        this.$router.push({name: tab.name});
      }
    },
    getSou() {
      this.$router.push({path: '/sou', query: {name: this.name}});
    },
    logout() {
      localStorage.removeItem('honey-user') // 清除当前的token和用户的数据
      this.$router.push('/login')
    },
    handleFull() {
      document.documentElement.requestFullscreen()
    },
  },
  mounted() {
    this.activeName = this.$route.name;
  }
}
</script>

<style scoped>
.el-header {
  background-color: #B3C0D1;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.nav-bar {
  background-color: #333;
}

::v-deep .el-tabs__item {
  color: red !important; /* 修改标签页文字颜色 */
}

::v-deep .el-tabs__item.is-active {
  color: blue !important; /* 修改活动标签页文字颜色 */
}
</style>